<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改地址</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/addAddress.css">
    <link rel="stylesheet" href="css/registerCommon.css">
</head>
<body>
<div class="nav">电子物品交易</div>
<div class="container">
    <!-- 登录对话框 -->
    <div class="dialog">
        <h2>修改地址</h2>

        <div class="row">
            <span>收货人</span>
            <input type="text" id="consignee" placeholder="收货人">
        </div>

        <div class="row">
            <span>联系方式</span>
            <input type="number" id="contactPhone" placeholder="请输入联系方式">
        </div>

        <div class="row">
            <span>省份</span>
            <input type="text" id="province" placeholder="请输入省份">
        </div>
        <div class="row">
            <span>城市</span>
            <input name="text" id="city" placeholder="请输入城市" type="text" >
        </div>
        <div class="row">
            <span>区</span>
            <input name="text" id="area" placeholder="请输入所在区" type="text" >
        </div>
        <div class="row">
            <span>乡/镇/街道</span>
            <input name="text" id="town" placeholder="请输入乡/镇/街道" type="text" >
        </div>
        <div class="row" style="height: 78px">
            <span>详细地址</span>
            <textarea name="text" id="detail" placeholder="请输入详细地址"></textarea>
        </div>
        <div class="row">
            <button class="submit" onclick="updateAddress()">提交</button>
        </div>
    </div>

</div>

<script src="js/jquery.min.js"></script>
<script src="js/userAddress.js"></script>
<script>
    let userId = localStorage.getItem("userId");
    if (userId === null || userId === "") {
        alert("请先登录");
        location.href = "/login.html";
    }
    let queryString = location.search;
    // 创建 URLSearchParams 对象
    let urlParams = new URLSearchParams(queryString);
    let addressId = urlParams.get("addressId");
    getAddressById(addressId, showAddressInfo);
    function showAddressInfo(address) {
        $("#consignee").val(address.consignee);
        $("#contactPhone").val(address.contactPhone);
        let ps = (address.addressDetail).split(" ");
        $("#province").val(ps[0]);
        $("#city").val(ps[1]);
        $("#area").val(ps[2]);
        $("#town").val(ps[3]);
        $("#detail").val(ps[4]);
    }
    function updateAddress() {
        //校验参数
        let pass = true;
        $(' input').each(function () {
            let inputValue = $(this).val();
            if (inputValue === null || inputValue === "") {
                const labelText = $(this).prev('span').text();
                pass = false;
                alert("请输入" + labelText);
                return false;
            }
        });
        if (!pass) {
            return;
        }
        $.ajax({
            type: 'post',
            url: '/userAddress/updateById',
            data: {
                addressId: addressId,
                userId: userId,
                consignee: $("#consignee").val(),
                contactPhone: $("#contactPhone").val(),
                province: $("#province").val(),
                city: $("#city").val(),
                area: $("#area").val(),
                town: $("#town").val(),
                detail: $("#detail").val()
            }
            ,success: function(result) {
                if(result.code === "SUCCESS") {
                    alert("修改成功");
                    history.back();
                }else{
                    alert(result.errMsg);
                }
            }
            ,error: function(error) {
                if (error != null && error.status === 401) {
                    alert("请先登录");
                    location.href = "/login.html";
                } else {
                    alert("内部错误");
                }
            }
        });
    }
</script>
</body>
</html>